package com.alibaba.chaosblade.box.controller.agent;

import com.alibaba.chaosblade.box.annotation.AgentEndPoint;
import com.alibaba.chaosblade.box.common.common.domain.response.Response;
import com.alibaba.chaosblade.box.controller.BaseController;
import com.alibaba.chaosblade.box.service.ChaosAgentCallbackService;
import com.alibaba.chaosblade.box.service.model.agent.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@Slf4j
public class AgentCallbackController extends BaseController {

    @Autowired
    private ChaosAgentCallbackService chaosAgentCallbackService;

    /**
     * 处理Agent注册请求
     *
     * @param registeredCallbackRequest Agent注册请求参数，包含IP地址、端口、CPU数量、内存大小、进程ID、操作系统版本等信息
     * @return 返回注册结果，包含配置ID、ChaosBlade版本、MD5值以及认证信息
     */
    @PostMapping(value = "AgentRegister")
    @AgentEndPoint
    public Response<ChaosAgentRegisterResultEntity> registerAgent(
            @RequestBody RegisteredCallbackRequest registeredCallbackRequest) {
        return chaosAgentCallbackService.handleAgentRegisterRequest(registeredCallbackRequest);
    }

    /**
     * 处理Agent心跳请求
     *
     * @param heartBeatCallbackRequest Agent心跳请求参数，包含版本号、Chaosblade版本、进程ID和IP地址等信息
     * @return 返回心跳处理结果，布尔类型表示处理成功或失败
     */
    @PostMapping(value = "AgentHeartBeat")
    @AgentEndPoint
    public Response<Boolean> agentHeartBeat(
            @RequestBody HeartBeatCallbackRequest heartBeatCallbackRequest) {
        return chaosAgentCallbackService.handleAgentHeartBeat(heartBeatCallbackRequest);
    }

    /**
     * 处理Agent关闭回调请求
     *
     * @param agentClosedCallbackRequest Agent关闭回调请求参数，包含Chaosblade版本、进程ID和版本等信息
     * @return 返回关闭回调处理结果，布尔类型表示处理成功或失败
     */
    @PostMapping(value = "AgentClosed")
    @AgentEndPoint
    public Response<Boolean> agentClosedCallback(
            @RequestBody AgentClosedCallbackRequest agentClosedCallbackRequest) {
        return chaosAgentCallbackService.handleAgentClosedRequest(agentClosedCallbackRequest);
    }

    /**
     * 处理Java Agent安装请求
     *
     * @param agentMetricRequest Agent指标请求参数，包含指标信息、设备配置ID、用户ID、状态和错误信息等
     * @return 返回Java Agent安装处理结果
     */
    @PostMapping(value = "javaAgentInstall")
    @AgentEndPoint
    public Response<String> javaAgentInstall(
            @RequestBody AgentMetricRequest agentMetricRequest) {
        return chaosAgentCallbackService.handleJavaAgentInstall(agentMetricRequest);
    }


    /**
     * 处理K8s Pod指标请求
     *
     * @param k8sPodsMetricRequest K8s Pods指标请求参数，包含SK、进程ID、端口、版本、类型和Pods信息等
     * @return 返回处理结果，Map类型包含键值对形式的结果信息
     */
    @PostMapping(value = "k8sPod")
    @AgentEndPoint
    public Response<Map<String, String>> k8sPod(@RequestBody K8sPodsMetricRequest k8sPodsMetricRequest) {

        return chaosAgentCallbackService.handlerPodMetric(k8sPodsMetricRequest);
    }

    /**
     * 处理Chaosblade异步请求
     *
     * @param chaosbladeAsyncRequest Chaosblade异步请求参数，包含状态、错误信息和工具类型等
     * @return 返回异步请求处理结果
     */
    @PostMapping(value = "chaosbladeAsync")
    @AgentEndPoint
    public Response<String> chaosbladeAsync(@RequestBody ChaosbladeAsyncRequest chaosbladeAsyncRequest) {

        return chaosAgentCallbackService.handlerChaosbladeAsync(chaosbladeAsyncRequest);
    }

}
